<?php
/**
 * Pages_Model_Student
 *
 */
class Zend_Student extends Zend_Db_Table_Abstract 
{

    protected $_name = 'T_STUDENTS';
    protected $_name_gr = 'T_PG_TASKS_GROUPS';
    protected $_name_teach = 'T_TEACHERS';
    protected $_name_tsk = 'T_PG_TASKS';
    protected $_name_tsks = 'T_TASKS';
    protected $_name_vr = 'T_VAR_DATA';

    /**
     * Get student's data
     * 
     * @param int $studentId
     */
    public function getStudent($studentId) 
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name, array('NAME_FIRST', 'NAME_MIDDLE', 'NAME_LAST', 'BOOK_NUMBER', 'GROUP_ID'))
            ->where('T_STUDENTS.ID = ?', $studentId)
            ->join('T_STUDENTS_GROUPS', 'T_STUDENTS.GROUP_ID = T_STUDENTS_GROUPS.ID', array('ID', 'NAME', 'TEACHER_ID'))
            ->order('ID DESC')	
            ->limit(1);
        $stmt = $this->getAdapter()->query($select);
        $result = $stmt->fetch();
        return $result;
    }

    /**
     * Check if student have a selected task
     * 
     * @param int $studentId
     */
    public function checkStudent($studentId) 
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name_gr)
            ->where('T_PG_TASKS_GROUPS.RESERVED_FOR = ?', $studentId)
            ->order('ID DESC')
            ->limit(1);
        $stmt = $this->getAdapter()->query($select);
        $result = $stmt->fetch();
        return $result;
    }

    /**
     * Get student's ID acoording to book number
     * 
     * @param int $number
     */
    public function getId($number)
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name)
            ->where('T_STUDENTS.BOOK_NUMBER = ?', $number)
            ->order('ID DESC')
            ->limit(1);
        $stmt = $this->getAdapter()->query($select);
        $result = $stmt->fetch();
        if ($result != NULL)
        return $result['ID'];
        else return NULL;
    }

    /**
     * Get available tasks for current group
     * 
     * @param int $groupId
     */
    public function availableTasks ($groupId)
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name_gr, array('ID'))
            ->where('T_PG_TASKS_GROUPS.ASSIGNED_TO = ?', $groupId)
            ->where('T_PG_TASKS_GROUPS.IS_ASSIGNED = ?', 0)
            ->order('ID DESC');
        $stmt = $this->getAdapter()->query($select);
        $tasks = $stmt->fetchAll();
        for ($i=0; $i<count($tasks); $i++) 
        {
            $currentId=$tasks[$i]['ID'];
            $task_info=$this->getTask($currentId);
            $result[] = array ('ID'=>$currentId, 'n1'=>$task_info[0] ['name'], 'n2'=>$task_info[1] ['name'], 'img1'=>$task_info[0] ['image'], 'img2'=>$task_info[1] ['image'], 'v1'=>$task_info[0] ['var'], 'v2'=>$task_info[1] ['var'], 'vd1'=>$task_info[0] ['var_data'], 'vd2'=>$task_info[1] ['var_data']);
        }
	return $result;
    }

    /**
     * Get task data
     * 
     * @param int $taskId
     */
    public function getTask($taskId) 
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name_tsk)
            ->where('T_PG_TASKS.GROUP_ID = ?', $taskId)
            ->limit(2);
        $stmt = $this->getAdapter()->query($select);
        $taskNumbers = $stmt->fetchAll();
        $result=$taskNumbers;
        $Id[0]=$taskNumbers[0] ['TASK_ID'];
        $Id[1]=$taskNumbers[1]['TASK_ID'];
        $v[0]=$taskNumbers[0]['VAR_NUMBER'];
        $v[1]=$taskNumbers[1]['VAR_NUMBER'];
        for ($i=0; $i<2; $i++) 
        {
            $select1 = $this->getAdapter()->select()
                ->from($this->_name_tsks)
                ->where('T_TASKS.ID = ?', $Id[$i])
                ->limit(1);
            $stmt1 = $this->getAdapter()->query($select1);
            $tasks = $stmt1->fetch();
            $name=$tasks['NAME'];
            $image=$tasks['IMAGE'];
            $select2= "select VAR_DATA from T_VAR_DATA where TASK_ID=$Id[$i] and VAR_NUMBER=$v[$i]";
            $stmt2 = $this->getAdapter()->query($select2);
            $variants = $stmt2->fetch();
            $var_data=$variants['VAR_DATA'];
            $result[$i]=array('name' => $name, 'image' => $image, 'var' => $v[$i], 'var_data' => $var_data);
        }
        return $result;
    }

    /**
     * Update task record as selected by a student
     * 
     * @param int $studentId
     * @param int $taskId
     */
    public function insertTask($studentId, $taskId) 
    {
        $date=time();
        $data = array(
            'IS_ASSIGNED'      => 1,
            'ASSIGMENT_DATE'      => $date,
             'RESERVED_FOR'      => $studentId
        );
        $where[] = "ID = $taskId";
        $n = $this->getAdapter()->update('T_PG_TASKS_GROUPS', $data, $where);
        return 1;
    }

    /**
     * Get teacher data
     * 
     * @param int $teachId
     */
    public function getTeacher($teachId) 
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name_teach)
            ->where('T_TEACHERS.ID = ?', $teachId)
            ->limit(1);
        $stmt = $this->getAdapter()->query($select);
        $result = $stmt->fetch();
        return $result;
    }
}